from five import grok
from zope import schema
from plone.directives import form

from plone.app.textfield import RichText

from plumvillage.registration import PlumVillageMessageFactory as _

# Indexer
from plone.indexer import indexer

# RoomFolder list
from zope.schema.vocabulary import SimpleVocabulary
from Products.CMFCore.utils import getToolByName
from zope.schema.interfaces import IContextSourceBinder
from plumvillage.registration.roomfolder import IRoomFolder

@grok.provider(IContextSourceBinder)
def availableRoomFolders(context):
    """ Return a catalog search result of available room folders
    """

    catalog = getToolByName(context, 'portal_catalog')
    results = catalog({'object_provides': IRoomFolder.__identifier__,
                       'sort_on' : 'sortable_title'})
    terms = []

    if results is not None:
        for roomfolder in results:
            terms.append(SimpleVocabulary.createTerm(roomfolder.getId, roomfolder.Title))
    
    return SimpleVocabulary(terms)

class IRegistration(form.Schema):
    """A registration, which also may contain reservations and invoices
    """

    roomfolder = schema.Choice(
            title=_(u"Hamlet"),
            source=availableRoomFolders,
            required=False
        )

    lastname = schema.ASCIILine(
            title=_(u"Last Name"),
            description=_(u""),
            required=True
        )

    firstname = schema.ASCIILine(
            title=_(u"First Name"),
            description=_(u""),
            required=True
        )

    address = schema.ASCIILine(
            title=_(u"Address"),
            description=_(u""),
            required=True
        )

    city = schema.ASCIILine(
            title=_(u"City"),
            description=_(u""),
            required=True
        )

    postalcode = schema.ASCIILine(
            title=_(u"Postal Code"),
            description=_(u""),
            required=False
        )

class View(grok.View):
    """Default view (called "@@view") for a registration.

    The associated template is found in registration_templates/view.pt.
    """

    grok.context(IRegistration)
    grok.require('zope2.View')
    grok.name('view')
